package com.example.module_patrol.ui

import androidx.lifecycle.MutableLiveData
import com.example.lib_common.base.BaseViewModel
import com.example.module_patrol.model.PatrolEntity
import com.example.module_patrol.model.PatrolNotDoingEntity
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
@HiltViewModel
class PatrolViewmodel @Inject constructor(private val repository: PatrolRepository):BaseViewModel() {
    private val patrolList by lazy {
        MutableLiveData<List<PatrolEntity>>()
    }
    val patrolListLiveData get() = patrolList

    private val addPatrol by lazy {
        MutableLiveData<PatrolEntity>()
    }
    val addPatrolLiveData get() = addPatrol

    private val searchPatrol by lazy {
        MutableLiveData<List<PatrolEntity>>()
    }
    val searchPatrolLiveData get() = searchPatrol

    private val patrolTaskList by lazy {
        MutableLiveData<List<PatrolNotDoingEntity>>()
    }
    val patrolTaskListLiveData get() = patrolTaskList

    private val getPatrolById by lazy {
        MutableLiveData<PatrolEntity>()
    }
    val getPatrolByIdLiveData get() = getPatrolById

    private val patrolTaskListDate by lazy {
        MutableLiveData<List<PatrolNotDoingEntity>>()
    }
    val patrolTaskListDateLiveData get() = patrolTaskListDate

    fun getPatrolList(){
        execute(
            action = repository.getParkingList(),
            onError = {
                patrolList.value = null
            },
            onFinish = {
                patrolList.value = it.data
            }
        )
    }
    fun addPatrol(patrolEntity: PatrolEntity){
        execute(
            action = repository.addPatrol(patrolEntity),
            onError = {
                addPatrol.value = null
            },
            onFinish = {
                addPatrol.value = it.data
            }
        )
    }
    fun searchPatrol(location:String,name:String){
        execute(
            action = repository.searchPatrol(location,name),
            onError = {
                searchPatrol.value = null
            },
            onFinish = {
                searchPatrol.value = it.data
            }
        )
    }
    fun getPatrolTaskByStatus(status:String){
        execute(
            action = repository.getPatrolTaskByStatus(status),
            onError = {
                patrolTaskList.value = null
            },
            onFinish = {
                patrolTaskList.value = it.data
            }
        )
    }
    fun getPatrolById(id:Int){
        execute(
            action = repository.getPatrolById(id),
            onError = {
                getPatrolById.value = null
            },
            onFinish = {
                    getPatrolById.value = it.data

            }
        )
    }
    fun getPatrolTaskByDate(startDate:String,end:String){
        execute(
            action = repository.getPatrolTaskByDate(startDate,end),
            onError = {
                patrolTaskListDate.value = null
            },
            onFinish = {
                patrolTaskListDate.value = it.data
            }
        )
    }
}